/**
 * 计数器
 */

// 简单计数器
function SimpleCounter() {
    var cnt = 0
    return () => {
        cnt = cnt + 1
        return cnt
    }
}

var c1 = SimpleCounter()
println(c1())
println(c1())
println(c1())

var c2 = SimpleCounter()
println(c2())
println(c2())
println(c2())

println(c1())
println(c2())

// 计数器
function Counter(init) {
    var cnt = init
    return {
        // 获取当前计数值
        current: () => cnt,
        // 计数值+1
        inc: () => {cnt++},
        // 计数值-1
        dec: () => {cnt--}
    }
}

var c3 = Counter(100)
println(c3.current()) // 100
c3.inc()
println(c3.current()) // 101
c3.inc()
println(c3.current()) // 102
c3.dec()
println(c3.current()) // 101
c3.dec()
println(c3.current()) // 100

var c4 = Counter(200)
println(c4.current()) // 200
c4.inc()
println(c4.current()) // 201
c4.inc()
println(c4.current()) // 202
c4.dec()
println(c4.current()) // 201
c4.dec()
println(c4.current()) // 200
